// axios的封装 为了可以方便的统一的使用axios做数据请求
import axios from 'axios'
import qs from 'qs'
import store from '../store/index'
import router from '@/router/index.js'
import { Loading } from 'element-ui';
const service = axios.create({
  withCredentials: true,
  baseURL: "http://39.100.147.206:8056",
  // baseURL: "http://192.168.1.102:8028",
  timeout: 8000
})
service.interceptors.request.use(

  config => {

    // let token = getToken() || "";
    config.headers = {
      'Content-Type': 'application/json;charset=UTF-8',
      // Authentication: token
    }
    store.commit('handLoad', true)
    // startLoading()
    return config;
  },
  error => {
    return Promise.reject(err);
  }
);
// 封装响应拦截，判断token是否过期
service.interceptors.response.use(
  response => {
    // this.$store.commit('handLoad', false)
    // this.$store.state.loading = false
    store.commit('handLoad', false)
    return Promise.resolve(response);
  },
  error => {
    console.log("错误", error.response)
    // if (error.response.status == 401) {
    //   router.replace({
    //     path: "/login",
    //   });
    // }

    return Promise.reject(error);
  }
);
function http (config) {
  if (config.method.toLowerCase() === 'post') {
    config.data = qs.stringify(config.data, { arrayFormat: 'repeat', allowDots: true, allowDots: true })
  } else {
    config.params = config.data
  }
  return service(config)
}
export default http
let loading

function startLoading () {
  this.$store.commit('handLoad', true)
}

function endLoading () {
  this.$store.commit('handLoad', false)
}